package com.rykj.energy.basic.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rykj.energy.common.utils.Result;
import com.rykj.energy.database.pojo.BasicMeter;
import com.rykj.energy.database.pojo.BusinessRotation;
import com.rykj.energy.database.pojo.BusinessUserConfig;
import com.rykj.energy.database.pojo.vo.MeterInfoVo;
import com.rykj.energy.database.service.IMeterService;
import com.rykj.energy.database.service.IUserConfigSercive;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "仪表设备管理")
@Slf4j
@RestController
@RequestMapping("/meter")
public class MeterController {

    @Autowired
    private IMeterService meterService;

    @Autowired
    private IUserConfigSercive userConfigSercive;

    @ApiOperation("获取首页展示的仪表")
    @GetMapping("/getShowPageMeters")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId",value = "区域id",paramType = "query"),
    })
    public Result<List<BasicMeter>> getShowPageMeters(@RequestParam(value = "orgId",required = false) Integer orgId){
        try {
          List<BasicMeter>  meters =meterService.getShowPageMeters(orgId);
          return Result.success(meters);
        }catch (Exception e){
            log.error("===获取首页展示的仪表异常===",e);
            return Result.failure("获取异常");
        }
    }

    @ApiOperation("保存仪表信息")
    @PostMapping("/saveMeter")
    public Result saveMeter(@RequestBody BasicMeter basicMeter){
        try {
            meterService.saveMeter(basicMeter);
        }catch (Exception e){
            log.error("===新增系统异常===",e);
           return Result.failure("新增系统异常");
        }
        return Result.success("保存成功",null);
    }

    @ApiOperation("修改仪表信息")
    @PostMapping("/updateMeter")
    public Result updateMeter(@RequestBody BasicMeter basicMeter){
        try {
            meterService.updateMeter(basicMeter);
        }catch (Exception e){
            log.error("===修改系统异常===",e);
            return Result.failure("修改系统异常");
        }
        return Result.success("保存成功",null);
    }

    @ApiOperation("删除仪表")
    @GetMapping("/deleteMeter")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meterId",value = "仪表id",required = true,paramType = "query"),
    })
    public Result deleteMeters(@RequestParam(value = "meterId") Integer meterId){
        try {
            meterService.deleteMeters(meterId);
        }catch (Exception e){
            log.error("===删除系统异常===",e);
            return Result.failure("删除系统异常");
        }
        return Result.success("删除成功",null);
    }


    @ApiOperation("获取组织下的仪表列表-分页（递归子组织）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "组织id", paramType = "query"),
            @ApiImplicitParam(name = "searchName", value = "仪表名称（模糊匹配）", paramType = "query"),
            @ApiImplicitParam(name = "meterType", value = "仪表类型", paramType = "query")
    })
    @GetMapping("/getMeterPages/{pageNum}/{pageSize}")
    public Result<IPage<MeterInfoVo>> getMeterPages(@PathVariable("pageNum")Integer pageNum,
                                                    @PathVariable("pageSize")Integer pageSize,
                                                    @RequestParam(value = "orgId",required = false)  Integer orgId,
                                                    @RequestParam(value = "searchName",required = false) String searchName ,
                                                    @RequestParam(value = "meterType",required = false) String meterType) {
        try {
            IPage<MeterInfoVo> page=null;
            page = meterService.getMeterPages(pageNum,pageSize,orgId,searchName,meterType);
            return   Result.success(page);
        } catch (Exception e) {
            log.error("获取区域下的仪表列表异常", e);
            return Result.failure("获取区域下的仪表列表异常");
        }
    }



    @ApiOperation("获取未选择实时轮播的仪表-分页（递归子组织）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "组织id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "searchName", value = "仪表名称（模糊匹配）", paramType = "query"),
            @ApiImplicitParam(name = "meterType", value = "仪表类型", paramType = "query")
    })
    @GetMapping("/getNotRotationMeters/{pageNum}/{pageSize}/{orgId}")
    public Result<IPage<BasicMeter>> getNotRotationMeters(@PathVariable("pageNum")Integer pageNum,
                                @PathVariable("pageSize")Integer pageSize,
                                @PathVariable("orgId") Integer orgId,
                                @RequestParam(value = "searchName",required = false) String searchName ,
                                 @RequestParam(value = "userId") Integer userId,
                                @RequestParam(value = "meterType",required = false) String meterType) {
        try {
            IPage<BasicMeter> page=null;
            page = meterService.getNotRotationMeters(pageNum,pageSize,orgId,searchName,meterType,userId);
            return   Result.success(page);
        } catch (Exception e) {
            log.error("获取未选择实时轮播的仪表异常", e);
            return Result.failure("获取未选择实时轮播的仪表异常");
        }
    }

    @ApiOperation("获取实时轮播展示的仪表及相关配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id",required = true,  paramType = "query")
    })
    @GetMapping("/getRotationMeters")
    public Result<BusinessUserConfig> getRotationMeters(@RequestParam(value = "userId") Integer userId) {
        try {
            BusinessUserConfig userConfig = userConfigSercive.findUserConfig(userId);
            List<BusinessRotation> retMaps = meterService.getRotationByUserId(userId);
            if(userConfig!=null){
                userConfig.setBusinessRotations(retMaps);
            }
            return   Result.success(userConfig);
        } catch (Exception e) {
            log.error("获取轮播展示仪表异常", e);
            return Result.failure("获取轮播展示仪表异常");
        }
    }

    @ApiOperation("分页获取实时轮播展示的仪表及最新的识别结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "组织id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户Id",  paramType = "query")
    })
    @GetMapping("/getRotationMetersResult/{pageNum}/{pageSize}")
    public Result<IPage<BusinessRotation>> getRotationMetersResult(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,
                                          @RequestParam(value = "orgId",required = true) Integer orgId,
                                          @RequestParam(value = "userId",required = true) Integer userId) {

        try {
            IPage<BusinessRotation> businessRotationIPage = meterService.getRotationPageByUserId(pageNum,pageSize,orgId,userId);
            return Result.success(businessRotationIPage);
        } catch (Exception e) {
            log.error("获取轮播展示仪表异常", e);
            return Result.failure("获取轮播展示仪表异常");
        }
    }

    @ApiOperation("保存实时轮播展示的仪表及相关配置")
    @PostMapping("/saveRotationMeters")
    public Result saveRotationMeters(@RequestBody BusinessUserConfig userConfig) {

        try {
            userConfig.setConfigNo(1000);//默认实时仪表配置是1000
            userConfigSercive.saveOrUpdateUserConfig(userConfig);
            meterService.saveRotationMeters(userConfig.getUserId(),userConfig.getBusinessRotations());
            return   Result.success("保存成功",null);
        } catch (Exception e) {
            log.error("保存实时轮播展示的仪表异常", e);
            return Result.failure("保存实时轮播展示的仪表异常");
        }
    }

    @ApiOperation("调整实时轮播的展示顺序")
    @PostMapping("/saveRotationOrderBy")
    public Result saveRotationOrderBy(@RequestBody List<BusinessRotation> rotations) {
        try {
            meterService.updateRotationMeters(rotations);
            return Result.success("保存成功",null);
        } catch (Exception e) {
            log.error("调整实时轮播的展示顺序异常", e);
            return Result.failure("调整实时轮播的展示顺序异常");
        }
    }

    @ApiOperation(value = "删除实时轮播展示的仪表-支持批量",notes = "删除实时轮播展示的仪表-支持批量" )
    @GetMapping("/deleteRotationss")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rotationIds", value = "多个id逗号隔开", required = true,paramType = "query")
    })
    public Result deleteExecutorGroups( @RequestParam(value = "rotationIds") String rotationIds) {
        try {
            meterService.deleteExecutorGroups(rotationIds);
        }catch (Exception e){
            log.error("删除实时轮播展示的仪表失败",e);
            return Result.failure("删除实时轮播展示的仪表失败");
        }
        return Result.success("删除成功",null);
    }

    @ApiOperation(value = "获取任务配置的仪表",notes = "获取任务配置的仪表" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "区域id",paramType = "query"),
            @ApiImplicitParam(name = "searchName", value = "仪表名称",paramType = "query"),
            @ApiImplicitParam(name = "meterType", value = "仪表类型 液晶表:LCD,LED表:LED,指针表:POINTER,字轮式仪表:WHEEL,避雷表:KATE",paramType = "query")
    })
    @GetMapping("/getMeterForTasks/{pageNum}/{pageSize}")
    public Result<IPage<BasicMeter>> getMeterForTasks(@PathVariable("pageNum")Integer pageNum,
                                                     @PathVariable("pageSize")Integer pageSize,
                                                     @RequestParam(value = "orgId",required = false) Integer orgId,
                                                     @RequestParam(value = "searchName",required = false) String searchName,
                                                     @RequestParam(value = "meterType",required = false) String meterType) {
        try {
            IPage<BasicMeter> page=null;
           page= meterService.getMeterForTasks(pageNum,pageSize,orgId,searchName,meterType);
           return Result.success(page);
        }catch (Exception e){
            log.error("获取任务配置的仪表失败",e);
            return Result.failure("获取任务配置的仪表失败");
        }
    }

}
